//
//  CNRetweetViewController.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/4/5.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import SVProgressHUD

class CNRetweetViewController: CNBaseInputViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func setupNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "dismissAction")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "转发", style: UIBarButtonItemStyle.Plain, target: self, action: "publishAction")
        navigationItem.title = "转发微博"
    }
    override func publishAction() {
        let status = textView.emoticonsText()
        if status.characters.count > statusMaxCount {
            SVProgressHUD.showErrorWithStatus("转发文本超过限定长度", maskType: SVProgressHUDMaskType.Black)
            return
        }
        SVProgressHUD.showWithStatus("正在转发微博...", maskType: SVProgressHUDMaskType.Gradient)
        CNStatusViewModel.sharedInstance.reposeStatus(id!, status: status, comment: nil) { (error) -> () in
            if error != nil {
                SVProgressHUD.showErrorWithStatus("转发微博失败", maskType: SVProgressHUDMaskType.Black)
                CNPrint(item: "sendStatusAction: \(error)")
                return
            }
            SVProgressHUD.showSuccessWithStatus("转发微博成功", maskType: SVProgressHUDMaskType.Clear)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(500 * USEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                self.dismissAction()
            })
        }
    }
    override func textViewDidChange(textView: UITextView) {
        let text = textView.emoticonsText()
        let count = statusMaxCount - text.characters.count
        tipLabel.text = "\(count)"
        tipLabel.textColor = count < 0 ? UIColor.redColor() : UIColor.lightGrayColor()
    }

}



